package model.recipe.dao;

import java.util.List;

import model.recipe.dto.RecipeCmtDto;
import model.recipe.dto.RecipeDto;
import model.recipe.dto.StepDto;

import org.apache.ibatis.session.SqlSession;

import common.SearchDto;

public class RecipeDaoImpl implements RecipeDao {
	private SqlSession sqlSession;
	public void setSqlSession(SqlSession sqlSession) {
		this.sqlSession = sqlSession;
	}
	
	@Override
	public List<RecipeDto> recipeList(String search_option, String search, int start, int end) {
		RecipeDto dto = new RecipeDto();
		dto.setSearch_option(search_option);
		dto.setSearch(search);
		dto.setStart(start);
		dto.setEnd(end);
		
		List<RecipeDto> list;
		if(search_option.equals("multi")){	//이름+제목+내용 검색
			list = sqlSession.selectList("recipe.recipeListAll", dto);
		}else{
			list = sqlSession.selectList("recipe.recipeList", dto);
		}
		
		return list;
	}

	@Override
	public List<String> callTypeList() {
		List<String> list = sqlSession.selectList("recipe.callTypeList");
		return list;
	}

	@Override
	public List<String> callIngredientList() {
		List<String> list = sqlSession.selectList("recipe.callIngredientList");
		return list;
	}

	@Override
	public List<Integer> callTimeList() {
		List<Integer> list = sqlSession.selectList("recipe.callTimeList");
		return list;
	}

	@Override // 내 가  최근에 쓴 레시피 3개  ; lzgroove
	public List<RecipeDto> myListDate(String userid) {
		return sqlSession.selectList("recipe.myListDate", userid);
	}

	@Override // 내가 쓴 레시피중 평점 최고 레시피 3개  ; lzgroove
	public List<RecipeDto> myListRate(String userid) {
		return sqlSession.selectList("recipe.myListRate", userid);
	}
	
	
	@Override
	public int maxRecipeIdx() {
		int max_idx = sqlSession.selectOne("recipe.maxRecipeIdx");
		return max_idx;
	}

	@Override
	public void recipeInsert(RecipeDto recipeDto) {
		sqlSession.insert("recipe.recipeInsert", recipeDto);
	}

	@Override
	public void stepInsert(StepDto stepDto) {
		sqlSession.insert("recipe.stepInsert", stepDto);
	}

	@Override
	public RecipeDto recipeDetail(int idx) {
		RecipeDto dto = sqlSession.selectOne("recipe.recipeDetail", idx);
		return dto;
	}

	@Override
	public List<StepDto> stepDetail(int idx) {
		List<StepDto> list = sqlSession.selectList("recipe.stepDetail", idx);
		return list;
	}

	@Override
	public void recipeHitUp(int idx) {
		sqlSession.update("recipe.recipeHitUp", idx);
	}

	@Override
	public void recipeUpdate(RecipeDto recipeDto) {
		sqlSession.update("recipe.recipeUpdate", recipeDto);
	}

	@Override
	public void stepUpdate(StepDto stepDto) {
		sqlSession.update("recipe.stepUpdate", stepDto);
	}

	@Override
	public void recipeDelete(int idx) {
		sqlSession.delete("recipe.recipeDelete", idx);
	}

	@Override
	public void stepDelete(StepDto stepDto) {
		sqlSession.delete("recipe.stepDelete", stepDto);
	}

	@Override
	public int recipeCount(String search_option, String search) {
		RecipeDto dto = new RecipeDto();
		dto.setSearch_option(search_option);
		dto.setSearch(search);
		int count = 0; 
		if(search_option.equals("multi")){	//이름+제목+내용 검색
			count = sqlSession.selectOne("recipe.recipeCountAll", dto);
		}else{
			count = sqlSession.selectOne("recipe.recipeCount", dto);
		}
		return count;
	}

	@Override
	public List<RecipeDto> topRecipeList(SearchDto search) {		
		return sqlSession.selectList("recipe.topRecipeList", search);
	}

	@Override
	public List<RecipeDto> myListRateAll(String userid) {		
		return sqlSession.selectList("recipe.myListRateAll", userid);
	}

	@Override
	public RecipeDto recipeInfo(int recipe_idx) {
		return sqlSession.selectOne("recipe.recipeInfo", recipe_idx);
	}

	
	@Override
	public List<RecipeCmtDto> recipeCmtList(int idx) {
		List<RecipeCmtDto> list = sqlSession.selectList("recipe.recipeCmtList", idx);
		return list;
	}
	
	@Override
	public void recipeCmtInsert(RecipeCmtDto dto) {
		sqlSession.insert("recipe.recipeCmtInsert", dto);
	}

	@Override
	public void recipeCmtDelete(int cmtIdx) {
		sqlSession.delete("recipe.recipeCmtDelete", cmtIdx);
	}

	@Override
	public int stepCount(int idx) {
		int count = sqlSession.selectOne("recipe.stepCount", idx);
		return count;
	}
	
	
}
